﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class ClientOrder : Form
    {
        public ClientOrder()
        {
            InitializeComponent();
        }
        SqlDataAdapter adapter = null;
        DataSet ds = null;
       


        

        private void ToolStripButton7_Click(object sender, EventArgs e)
        {
            string connString = "Data Source=.;Initial Catalog =GXFactoryDB ;Integrated Security = True";
            SqlConnection connection = new SqlConnection(connString);
            connection.Open();
            
                string sql = "select *  from ClientOrder_tb ";
                ds = new DataSet();
                adapter = new SqlDataAdapter(sql, connection);
                adapter.Fill(ds, "ClientOrder_tb");
                dataGridView1.DataSource = ds.Tables["ClientOrder_tb"];
                connection.Close();
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            if (dataGridView1.CurrentRow.Selected) // 确保有选中的行
            {

                int i = dataGridView1.CurrentRow.Index;
                comboBox1.Text = dataGridView1.Rows[i].Cells["ForeignerAbbre"].Value.ToString();
                comboBox2.Text = dataGridView1.Rows[i].Cells["ClientAbbre"].Value.ToString();
                textBox1.Text = dataGridView1.Rows[i].Cells["OrderNo"].Value.ToString();
                textBox2.Text = dataGridView1.Rows[i].Cells["ProductNo"].Value.ToString();
                textBox3.Text = dataGridView1.Rows[i].Cells["ProductName"].Value.ToString();
                textBox4.Text = dataGridView1.Rows[i].Cells["Ordernumber"].Value.ToString();
                textBox5.Text = dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString();
                textBox6.Text = dataGridView1.Rows[i].Cells["Total"].Value.ToString();
                dateTimePicker1.Text= dataGridView1.Rows[i].Cells["OrderDate"].Value.ToString();
                dateTimePicker2.Text = dataGridView1.Rows[i].Cells["DeliveryDate"].Value.ToString();
                textBox7.Text = dataGridView1.Rows[i].Cells["Boxrate"].Value.ToString();
                textBox8.Text = dataGridView1.Rows[i].Cells["Boxes"].Value.ToString();
                comboBox3.Text = dataGridView1.Rows[i].Cells["ClientAbbre"].Value.ToString();
                comboBox4.Text = dataGridView1.Rows[i].Cells["ClientAbbre"].Value.ToString();
                comboBox5.Text = dataGridView1.Rows[i].Cells["ClientAbbre"].Value.ToString();
                textBox9.Text = dataGridView1.Rows[i].Cells["PackageNeed"].Value.ToString();
                textBox10.Text = dataGridView1.Rows[i].Cells["BasicNeed"].Value.ToString();
                textBox11.Text = dataGridView1.Rows[i].Cells["SpecialNeed"].Value.ToString();
            }
            }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            String connString = "Data Source=.;Initial Catalog =GXFactoryDB ;Integrated Security = True";
            SqlConnection connection = new SqlConnection(connString);
            connection.Open();
            string sql = "update ClientOrder_tb set ForeignerAbbre='"+comboBox1.Text+ "'";
            SqlCommand command = new SqlCommand(sql,connection);
            int i=command.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("保存成功");
                
            }
            else {
                MessageBox.Show("失败");
            }
        }

        

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            string connString = "Data Source=.;Initial Catalog =GXFactoryDB ;Integrated Security = True";
            SqlConnection connection = new SqlConnection(connString);
            connection.Open();

            string sql = "select *  from ClientOrder_tb ";
            ds = new DataSet();
            adapter = new SqlDataAdapter(sql, connection);
            adapter.Fill(ds, "ClientOrder_tb");
            dataGridView1.DataSource = ds.Tables["ClientOrder_tb"];
            connection.Close();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            String connString = "Data Source=.;Initial Catalog =GXFactoryDB ;Integrated Security = True";
            SqlConnection connection = new SqlConnection(connString);
            connection.Open();
            string sql = "update ClientOrder_tb set ForeignerAbbre='" + comboBox1.Text + "'";
            SqlCommand command = new SqlCommand(sql, connection);
            int i = command.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("修改成功");

            }
            else
            {
                MessageBox.Show("失败");
            }
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
            
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            String connString = "Data Source=.;Initial Catalog =GXFactoryDB ;Integrated Security = True";
            SqlConnection connection = new SqlConnection(connString);
            connection.Open();
            string sql = "insert into ClientOrder_tb(ForeignerAbbre,OrderNo) values('" + comboBox1+"','"+textBox2+"')";
            SqlCommand command = new SqlCommand(sql, connection);
            int i = command.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("添加成功");

            }

        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
